/****************************************************************************
* Copyright (c) 2014 Shijie. Dreamware(R) Inc. JuneStone Entertainment
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*****************************************************************************/

#ifndef __APP_CONFIG_H__
#define __APP_CONFIG_H__

#include <irrlicht.h>
#include <windows.h>
#include <autoref.h>
#include "object.h"
#include <map>
#include <xmltree.h>

using namespace irr;

struct st_config{ 
	io::path app_path;
	io::path cfg_path;
	io::path app_title;
	io::path audio_path;
	io::path font_path;
	core::dimension2d<u32> screen_resolution;
	bool is_fullscreen;

	video::E_DRIVER_TYPE video_api;
	u32 color_depth;
	bool is_vsync;
	bool is_stencilbuffer;

	st_config() :
		cfg_path(TEXT("\\profile\\tankconfig.xml")) ,
		app_title(TEXT("Tank Alpha -- Junestone Inc.")),
		audio_path(TEXT("\\audio")),
		font_path(TEXT("\\font")), 
		screen_resolution(core::dimension2d<u32>(800,600)),
		is_fullscreen(false),
		video_api(video::EDT_OPENGL),
		color_depth(32),
		is_vsync(true),
		is_stencilbuffer(false)
	{
		TCHAR _path[1024];
		int bytes = GetModuleFileName(NULL, _path, 1024);
		if (bytes > 0)
		{
			core::stringw path(_path);
			app_path = path.subString(0, path.findLast(L'\\'));  
		} 
	}
};

//XML file path config module
class AppConfig : public Object{
public:
	bool read();
	bool read(io::path path);

	bool save();
	bool save(io::path path); 
	bool save(XmlTree *tree);
 
	bool set_attribute(core::stringw, core::stringw);

	st_config& get_config();
	 
#ifdef _DEBUG
	bool make_config();
#endif

public:
	AppConfig();
	virtual ~AppConfig();
	 
private: 
	st_config m_cfg;
    IrrlichtDevice *m_null_device;

	std::map<core::stringw, s32> m_driver_options;  
	std::map<core::stringw, core::dimension2du> m_resolution_options;  
	std::map<core::stringw, core::stringw> m_core_config;
};

#endif